package com.sghd.fight.module.game.service.configure;

import com.sghd.common.utils.math.RandomUtils;
import com.sghd.fight.module.game.config.ConfigNames;
import com.sghd.fight.module.game.config.ElementConfig;
import com.sghd.fight.module.game.config.ElementPk;
import com.sghd.fight.module.game.config.SkillConfig;
import com.sghd.fight.module.game.core.model.BattleSkill;
import com.sghd.fight.module.game.core.model.Unit;
import com.sghd.fight.module.game.service.room.IRoom;
import com.sghd.fight.module.game.service.room.Monster;
import com.sghd.fight.module.game.service.room.Player;
import com.sghd.fight.module.game.service.skill.ConfigService;
import com.sghd.fight.module.game.service.work.RoomTimer;
import com.sghd.pojo.fight.game.ChessType;
import com.sghd.pojo.fight.game.LevelVo;
import com.sghd.pojo.fight.game.PlayerVo;
import com.sghd.pojo.fight.game.UnitValue;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author frank
 * @date 2018-10-25 9:58
 * @desc
 **/
public abstract class ConfigureTemplate implements Configure {

    @Autowired
    protected ConfigService configService;

    @Override
    public Player transform(PlayerVo playerInfo, IRoom room, Date date) {
        Map<Integer, Integer> skill2Level = new HashMap<>();
        Set<Integer> randomResult = new HashSet<>();
        if (playerInfo.getSkills().size() < 5) {
            List<Integer> skills = new ArrayList<>(Arrays.asList(1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 10010,
                    10011, 10012, 10013, 10014, 10015, 10016, 10017, 10018, 10019, 10020, 10021, 10022, 10023, 10024, 10025, 10026, 10028, 10029, 10030, 10031, 10032,
                    10033, 10034, 10035, 10036, 10037, 10038, 10039));
            //TODO 测试，所有默认0级
            for (Integer skill : skills) {
                skill2Level.put(skill, 0);
            }
            Collections.shuffle(skills);
            //随机取5个技能
            randomResult = new HashSet<>(skills.subList(0, 5));
        } else {
            Integer[] playerOwnerSkills = playerInfo.getSkills().keySet().toArray(new Integer[0]);
            for (Integer ownerSkillId : playerOwnerSkills) {
                skill2Level.put(ownerSkillId, playerInfo.getSkills().get(ownerSkillId));
            }
            while (randomResult.size() < 5) {
                Integer skillId = playerOwnerSkills[RandomUtils.nextInt(playerOwnerSkills.length)];
                randomResult.add(skillId);
            }
        }
        Player player = new Player(playerInfo.getPlayerId(), playerInfo.getName(), playerInfo.getIndex(), room,
                new HashMap<>(skill2Level), new HashMap<>(playerInfo.getElements()), playerInfo.getMoney());
        Map<Integer, BattleSkill> battleSkill = player.getSkills();
        for (Integer skillId : randomResult) {
            int level = skill2Level.get(skillId) == null ? 0 : skill2Level.get(skillId);
            SkillConfig skillConfig = configService.getSkillConfig(skillId);
            battleSkill.put(skillId, new BattleSkill(skillId, skillConfig.getCool(player.getSkillLevel(skillId)), level));
        }
        fixValue(player, room, playerInfo);
        return player;
    }

    /** 修改玩家属性*/
    public void fixValue(Unit unit, IRoom room, PlayerVo playerVo) {
        Player player = (Player) unit;
        Map<ChessType, Integer> attr2Level = player.getElement2Level();
        //五行累计提供的血量上限
        int total = 0;
        for (ChessType chessType : ChessType.values()) {
            if (chessType == ChessType.CT_LIGHTNING || attr2Level.get(chessType) == null || attr2Level.get(chessType) == 0) {
                continue;
            }
            int level = attr2Level.get(chessType);
            for (; level > 0; level--) {
                ElementPk pk = ElementPk.valueOf(chessType, level);
                ElementConfig attrConfig = configService.getAttr(pk);
                total += attrConfig.getAttr(room.getType()).getUpHp();
            }
        }
        Map<UnitValue, Integer> values = unit.getValues();
        int defaultHp = Integer.parseInt(configService.getGameConfig(ConfigNames.PVP_PLAYER_HP));
        values.put(UnitValue.HP, defaultHp + total);
        values.put(UnitValue.HP_MAX, defaultHp + total);
    }

    @Override
    public IRoom build(long roomId, RoomTimer timer) {
        return null;
    }

    @Override
    public IRoom build(long roomId, RoomTimer timer, LevelVo levelVo) {
        return null;
    }

    @Override
    public Monster transformByMonsterId(PlayerVo playerInfo, IRoom room, Date date) {
        return null;
    }

    @Override
    public ConfigService getConfigService() {
        return configService;
    }

    @Override
    public Player transformRobot(PlayerVo left, IRoom room, Date date) {
        return null;
    }
}
